<?php 
require_once("../includes/config.php"); 
  
  $date = date('Y-m-d');
 if(isset($_POST['payer']))
 {
    $query = query("INSERT INTO service_vouchers (owner_id,payer, amount ,paymen_method ,recipient
	                 ,miladi_date,reason,notes) VALUES (?,?,?,?,?,?,?,?)",$_SESSION['user_id'],$_POST['payer'],$_POST['amount'],$_POST['type'],$_POST['recipient'],$date,$_POST['reason'],$_POST['notes']);
                                      
    $_SESSION['success_message'] = "تم تحميل المبلغ إلى إرادات الخدمة";
    redirect("addService.php");
 }
 
 if(isset($_GET['contract_id']))
 {
   
    $query = query("SELECT service_name from services WHERE service_id = ?",$_GET['service']);
    $reason = $query[0]['service_name'];
    
    $query = query("INSERT INTO service_vouchers (contract_id , client_id, amount ,paymen_method ,recipient
	                 ,miladi_date,reason,owner_id) VALUES (?,?,?,?,?,?,?,?)",$_GET['contract_id'],$_GET['client_id'],$_GET['amount'],0,$_SESSION['login_user'],$date,$reason,$_SESSION['user_id']);
                   
    $result = query("UPDATE contracts SET services_revenue=services_revenue + ? where contract_id=?",$_GET['amount'],$_GET['contract_id']);
                   
    $_SESSION['success_message'] = "تم تحميل المصاريف على العقد ".$_GET['contract_id']." بنجاح";
    redirect("addService.php");
 }
 else
 {
  $servs=query("SELECT * FROM services WHERE owner_id =?",$_SESSION['user_id']);
  if(isset($_GET['id']))
    $sub_servs=query("SELECT * FROM sub_services WHERE service_id = ?",$_GET['id']);
  else if(!empty($servs))
    $sub_servs=query("SELECT * FROM sub_services WHERE service_id = ?",$servs[0]['service_id']);
  else
    $sub_servs = array();
      
  render("addService.php", array("title"=>"الخدمات","servs"=>$servs,"sub_servs"=>$sub_servs));
 }
 
?>